﻿using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using PMP.Core.Entities;
using PMP.Core.SqlSugar;

namespace PMP.Core.Extensions;

public static class CommonHelper
{
    /// <summary>
    /// 从文本中提取出 @人名
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static List<string> GetRealNameFromContent(string content)
    {
        var resList = new List<string>();
        string pattern = @"@(\S+)";
        // 匹配所有符合条件的结果
        MatchCollection matches = Regex.Matches(content, pattern);
        foreach (Match match in matches)
        {
            resList.Add(match.Groups[1].Value);
        }

        return resList;
    }
    
    /// <summary>
    /// 从文本中提取出 @人名
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static List<int> GetUserIdFromContent(string content)
    {
        var realNames = GetRealNameFromContent(content);
        if (!realNames.Any()) return new List<int>();
        var res =  DbContext.Instance.Queryable<TbAccount>()
            .Where(a => realNames.Contains(a.RealName))
            .Select(a => a.Id)
            .Distinct()
            .ToList();
        return res;
    }
}